/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#ifndef _INC_AK_CLI_
#define	_INC_AK_CLI_

#include "ak_types.h"
#include "ak_queue.h"
#include "ak_gears.h"

#ifdef __cplusplus
extern "C" {
#endif

#define S_CLI_MAX_CMD 		31
#define S_CLI_MAX_PARAM 	63
#define S_CLI_MAX_MSG 		0X4000

/* =====================================================================
** 	CLI Msg Definition:
**	All the Relay node should Route the Request/Response to the Next Hop.
*/

#define _CLI_CMD_QUIT 		0X0000
#define _CLI_CMD_CONNECT 	0X0001
#define _CLI_CMD_CD 		0X0002
#define _CLI_CMD_LS 		0X0003
#define _CLI_CMD_LCMD 		0X0004
#define _CLI_CMD_EXEC 		0X0005
#define _CLI_CMD_ERROR 		0X0010
#define _CLI_CMD_OK 		0X0011

/* -----------------------------------------------------------------------------
** Command Message Element.
** Command Message Header.
** 
*/
#define _CLI_MSG_ELEM_REQ 		0X00 		/* Message is Request */
#define _CLI_MSG_ELEM_RSP 		0X01 		/* Message is Response */
#define _CLI_MSG_ELEM_TRAP 		0X10 		/* Message is Trap */
#define _CLI_MSG_MAX 			0X20000

typedef struct{
	_t_sysMsgElem 		msgElem;
	char 				path[S_APP_MAX_PATH+ 1];
	char 				cmd[S_CLI_MAX_CMD + 1];
	char 				param[S_CLI_MAX_PARAM + 1];
}_t_cliElemReq;

typedef struct{
	_t_sysMsgElem 		msgElem;
	char 				reqRsp[0];	
}_t_cliElemRsp;

typedef struct{
	unsigned short 		port;
	unsigned short 		reserv;
	unsigned int 		ipAddr;
	unsigned int 		length;
	unsigned int 		padding;
}_t_cliCmdHdr;

typedef struct{
	_t_cliCmdHdr 		cmdHdr;
	_t_cliElemReq		reqElem[0];
}_t_cliCmdReq;

typedef struct{
	_t_cliCmdHdr 		cmdHdr;
	_t_cliElemRsp		rspElem[0];
}_t_cliCmdRsp;


/* CLI Port Define */
#define _S_CLI_PORT_SVR		5005
#define _S_CLI_PORT_CLT 	5050
#define _S_CLI_PORT_CNT		5

#define CLI_CMDHDR_INIT(vPHeader, vIpAddr, vPort) 	\
do{	\
	(vPHeader)->ipAddr 		= vIpAddr;	\
	(vPHeader)->port 		= vPort;	\
}while(0)


/* -----------------------------------------------------------------------------
** ANSI Color Command.
*/
#define _CLI_FMT_PROMPT_PATH 	"\033[0;;34m%s\033[0m# "
#define _CLI_FMT_APP_COMMAND 	"\033[0;;32m%s\033[0m\t"
#define _CLI_FMT_APP 			"\033[0;;33m%s\033[0m\t"
#define _CLI_FMT_APP_MODULE 	"[\033[0;;33m%-3d\033[0m]:%s\t"

/* =====================================================================
** CLI Interface:
*/
/*
**	ret =pCbEntry->cbProc(	pCbEntry->pArg,
**							pCbEntry->lArg,
**							outBuf,
**							outLen,
**							pParam);
**	pParam 	: Char *, User should parse it.
**	outBuf  : result buffer.
**	outLen 	: result buffer max length, User should Judge not to overflow it.
**	ret 	: result length. -1/0 means no result or fail. >0 means a result should be transfer out.
**
*/
typedef struct _t_cliCmdReg{
	char 			cmd[S_CLI_MAX_CMD + 1];
	_t_cbentry func;
}_t_cliCmdReg;

#define _S_CLI_CMD_HASH 		4

typedef struct _t_cliCommand{
	_t_bnode 		cmdNode;
	_t_bnode 		hashNode;

	_t_cliCmdReg 	cmdReg;
}_t_cliCommand;

typedef struct _t_cliCmdTbl{
	_t_blist 		cmdQue;
	_t_blist 		hashQue[_S_CLI_CMD_HASH];
}_t_cliCmdTbl;


#ifdef __cplusplus
}
#endif

#endif


